package csel.view;



import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import csel.model.EquippableItem;
import csel.model.GameCommand;
import csel.model.Model;
import csel.model.Item;
import csel.model.Entity;
import csel.model.Observer;
import csel.model.TakeableItem;

/**
 * Reasons for Existence:
 * Team Members Responsible
 * Implementation: Alex, Andrew
 * Testing: Alex, Andrew    
 */
public class StatusViewport extends Viewport implements Observer
{

	//the viewport's window
	JFrame myWindow;
	JMenuItem quitGame;
	JMenuItem newGame;
	JMenuItem saveGame;
	JMenuItem loadGame;
	JMenuBar menuButtons;
	JMenu menu;
	JPanel saPanel;

	//component Viewers
	AvatarViewer aViewer;
	EquipmentViewer eViewer;
	InventoryViewer iViewer;
	StatsViewer sViewer;

	private Model myModel;

	public StatusViewport(Model m)
	{
		TrampolineListener trampy = new TrampolineListener();
		myModel = m;
		myModel.addObserver(this);
		
		//create and define menu buttons
		quitGame = new JMenuItem("Quit");
		quitGame.setActionCommand("stquit");
		quitGame.addActionListener(trampy);

		newGame = new JMenuItem("New Game");
		newGame.setActionCommand("stnew");
		newGame.addActionListener(trampy);

		loadGame = new JMenuItem("Load Game");
		loadGame.setActionCommand("stload");
		loadGame.addActionListener(trampy);

		saveGame = new JMenuItem("Save Game");
		saveGame.setActionCommand("stsave");
		saveGame.addActionListener(trampy);
		

		myWindow = new JFrame("CSEL -- Status Viewer");
		menuButtons = new JMenuBar();
		menu = new JMenu("Game Options");
		saPanel = new JPanel();
		
		ImagePanel panel = new ImagePanel(new ImageIcon("images/statusbg.png").getImage());
		Container contentPane = myWindow.getContentPane();

		panel.setLayout(new GridBagLayout());
        //myWindow.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
		saPanel.setLayout(new BoxLayout(saPanel,BoxLayout.Y_AXIS));
		
		Border blackline = BorderFactory.createLineBorder(Color.black);

		
		//create and add viewers
		aViewer = new AvatarViewer(m.getPlayerEntity());
		eViewer = new EquipmentViewer(m.getPlayerEntity(),trampy);
		iViewer = new InventoryViewer(m.getPlayerEntity(),trampy);
		sViewer = new StatsViewer(m.getPlayerEntity());
		
        iViewer.setBorder(BorderFactory.createTitledBorder(blackline,"Inventory"));
        eViewer.setBorder(BorderFactory.createTitledBorder(blackline,"Equipment"));
        sViewer.setBorder(BorderFactory.createTitledBorder(blackline,"Stats"));
        aViewer.setBorder(BorderFactory.createTitledBorder(blackline,"Player"));
        
        menu.add(quitGame);
        menu.add(newGame);
        menu.add(loadGame);
        menu.add(saveGame);

		menuButtons.add(menu);
        
        aViewer.setOpaque(false);
        sViewer.setOpaque(false);
        
        saPanel.add(aViewer);
        saPanel.add(sViewer);
        
        c.fill = GridBagConstraints.VERTICAL;
        
        iViewer.setOpaque(false);
        saPanel.setOpaque(false);
        eViewer.setOpaque(false);
        
        panel.add(iViewer,c);
        panel.add(saPanel,c);
        panel.add(eViewer,c);
        myWindow.setJMenuBar(menuButtons);
        
        contentPane.add(panel);
        /*myWindow.add(iViewer,c);
        myWindow.add(saPanel,c);
        myWindow.add(eViewer,c);
        myWindow.add(menuButtons,c);*/
        
        myWindow.setSize(620,350);
        myWindow.setLocation(200,200);
        myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/*public void resetViewers()
	{
		TrampolineListener trampy = new TrampolineListener();
		aViewer = new AvatarViewer(myModel.getPlayerEntity());
		eViewer = new EquipmentViewer(myModel.getPlayerEntity(),trampy);
		iViewer = new InventoryViewer(myModel.getPlayerEntity(),trampy);
		sViewer = new StatsViewer(myModel.getPlayerEntity());
	}*/

	public void setVisible(boolean b)
	{
		myWindow.setVisible(b);
	}

	//needs to be updated
	public void close()
	{
		myWindow.dispose();
		System.out.println("[VIEW] StatusViewport closed!");
	}

	protected Container getContainer()
	{
		return myWindow;
	}
	
	public void accept(GameCommand g)
	{
		g.execute();
	}

	public TakeableItem[] getSelectedInventoryItems()
	{
		return iViewer.getSelectedItems();
	}
	
	public EquippableItem[] getSelectedEquipmentItems()
	{
		return eViewer.getSelectedItems();
	}
	
	public void modelUpdate(Object o)
	{
		Entity e = ((Model) o).getPlayerEntity();
		aViewer.setEntity(e);
		eViewer.setEntity(e);
		iViewer.setEntity(e);
		sViewer.setEntity(e);
	}
}
